﻿using System.Data.SqlClient;
using System.Data;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class gigdetails : System.Web.UI.Page
{
    VLGDataContext _db4 = new VLGDataContext();
    CommonPage cm66 = new CommonPage();
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["currentItem"] = "";
        //lblNgayComment.Text = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
        if (!IsPostBack)
        {
            
           // string idGURL = Request.QueryString["g"];
           // int tempDauTru = idGURL.LastIndexOf('-');
           // string idG = idGURL.Substring(tempDauTru + 1).Trim();
            
           // int intG2 = int.Parse(idG);
           ////loadFacebook();
           // Gig gig22 = _db4.Gigs.Single(idgg => idgg.idGig == intG2 && idgg.TrangThai == (int)EnumGigStatus.GigHien && idgg.Duyet == true);
           // //lbltags.Text = gig22.Tags;
           // lbltags.Text = "Tôi sẽ " + gig22.TenCongViec + " chỉ với 5 $";
           // lblGioithieu.Text = gig22.TuGioiThieu;
           // lblNgay.Text = gig22.SoNgayCanDeXong.ToString();
           // lblTagss.Text = gig22.Tags;
           // lblNguoidang.Text = gig22.VLGAccount.UsernameE;
           // lblNguoiPostLQ.Text = gig22.VLGAccount.UsernameE;
           // // Load Cờ
           // quocgia_s.Src = "img/flags/" + gig22.VLGAccount.ipAddress + ".png";
           // quocgia_s.Width = 20;
           // quocgia_s.Border = 0;

           // // load Like va DisLike
           // lblSLNLike.Text = gig22.SoLuotThich.ToString();
           // lblSLNDisL.Text = gig22.SoLuotKoThich.ToString();
           // // Loadi Don hang
           // var itemO = from or in _db4.Orders
           //             where or.idGig == intG2
           //             select or;
           // lblDonHang.Text = itemO.Count().ToString();
           // // Load Tittle
           // Page.Title = "Vieclamgap.net | " + gig22.Category.TenCate.ToUpper() + " | Tôi sẽ " + gig22.TenCongViec + " chỉ với 5$";
           // LiterTitle.Text = "\n<meta content=\"" + gig22.TenCongViec_KD + "," + gig22.Tags + "\" name=\"description\">" + "\n<meta content=\"" + gig22.TenCongViec_KD + "," + gig22.Tags + "\" name=\"content\">" + "\n<meta content=\"" + gig22.TenCongViec + "," + gig22.TenCongViec_KD + "," + gig22.Tags + "\" name=\"keywords\">";
           // // Avatar nguoi dang
           // if (!String.IsNullOrEmpty(gig22.VLGAccount.urlAvatar))
           // {
           //     avar_s.Src = "avatar/" + gig22.VLGAccount.urlAvatar;
           //     avar_s.Width = 50;
           //     avar_s.Height = 45;

           // }
           // else
           // {
           //     avar_s.Src = "avatar/noavatar.jpg";
           //     avar_s.Width = 50;
           //     avar_s.Height = 45;
           // }
           // var itemgig = from tc in _db4.Gigs
           //               where tc.idGig == intG2 //&& tc.AnHien==true
           //               select new { tc.idGig, tc.idAcc, tc.VLGAccount.UsernameE };
           // Repeater1.DataSource = itemgig;
           // Repeater1.DataBind();

           // if (String.IsNullOrEmpty(gig22.LinkVideo) == false)
           // {
           //     string[] tenfile = gig22.LinkVideo.Split('.');
           //     string tenfilekoduoi = tenfile[0];
           //     //antang.Src = "upload/" + gig22.UrlHinh1;
           //     //antang.Visible = false;
           //     string duongdanHinh = ResolveClientUrl("~/video/hinh.jpg");
           //     string duongDanMp4 = ResolveClientUrl("/videos/" + gig22.LinkVideo);
           //     string duongDanWebm = ResolveClientUrl("/video/" + tenfilekoduoi + ".webm");
           //     //Literal1.Text = "<script type=\"text/javascript\">jwplayer(\"myplayer\").setup({skin:\"/js/mediaplayer510/glow.zip\",stretching:\"fill\",flashplayer: \"/js/mediaplayer510/player.swf\",image: \"../videos/hinh.jpg\",width: 270,height: 210,levels: [{ file: \"videos/" + gig22.LinkVideo + "\" },{ file: \"videos/" + tenfilekoduoi + ".webm\" }]});</script>";
           //     Literal1.Text = "<script type=\"text/javascript\">jwplayer(\"myplayer\").setup({skin:\"http://content.longtailvideo.com/skins/glow/glow.zip\",stretching:\"fill\",flashplayer: \"http://player.longtailvideo.com/player.swf\",image: \"" + duongdanHinh + ",width: 270,height: 210,levels: [{ file: \"" + duongDanMp4 + "\" },{ file: \"" + duongDanWebm + "\"}]});</script>";
           // }
           // else
           // {
           //     string duongdan = ResolveClientUrl("~/upload/" + gig22.UrlHinh1);
           //     tamhinh.InnerHtml = "<img src=\"" + duongdan + "\" class=\"ava\" >";
           //     // antang.Src = "upload/" + gig22.UrlHinh1;
           //     // antang.Visible = false;
           // }
           // var itemCM = from cm in _db4.GigComments
           //              where cm.AnHien == true && cm.idGig == intG2
           //              select new { cm.idGig, cm.NoiDungBL, cm.NgayBL, cm.VLGAccount.UsernameE, cm.VLGAccount.idAcc, cm.Type };
           // //select cm;

           // SqlCommand cmd = (SqlCommand)_db4.GetCommand(itemCM);
           // DataTable dt = new DataTable();
           // SqlDataAdapter da = new SqlDataAdapter(cmd);
           // da.Fill(dt);

           // CollectionPager1.MaxPages = 1000;
           // CollectionPager1.PageSize = 7;
           // CollectionPager1.DataSource = dt.DefaultView;
           // CollectionPager1.BindToControl = DataList1;
           // DataList1.DataSource = CollectionPager1.DataSourcePaged;
           // DataList1.DataBind();
           // if (DataList1.Items.Count != 0)
           // {
           //     bian.Attributes.CssStyle.Add("display", "none");
           // }
           // // bai viet lien quan
           // var itemBVLQ = from bv in _db4.Gigs
           //                where bv.idAcc == gig22.idAcc && bv.idGig != gig22.idGig && bv.TrangThai == (int)EnumGigStatus.GigHien && bv.Duyet == true
           //                orderby bv.idAcc descending
           //                select new { bv.idGig, bv.TenCongViec, bv.Category.idCategory };
           // rptLienQuan.DataSource = itemBVLQ;
           // rptLienQuan.DataBind();

           // if (rptLienQuan.Items.Count != 0)
           // {
           //     bian2.Attributes.CssStyle.Add("display", "none");
           // }
            this.loadiDuLieuCT();
        }

    }
    void loadiDuLieuCT()
    {
        string idGURL = Request.QueryString["g"];
        int tempDauTru = idGURL.LastIndexOf('-');
        string idG = idGURL.Substring(tempDauTru + 1).Trim();
        //Response.Write(idGURL.Substring(tempDauTru + 1));
        //Gig dadc = _db4.Gigs.Single(lcaocac => lcaocac.idGig == int.Parse(idG));
        //lbltags.Text = "Tôi sẽ " + dadc.TenCongViec + " chỉ với 5$.";
        try
        {
            int intG2 = int.Parse(idG);

            Gig gig22 = _db4.Gigs.Single(idgg => idgg.idGig == intG2 && idgg.TrangThai == (int)EnumGigStatus.GigHien && idgg.Duyet == true);
            //lbltags.Text = gig22.Tags;
            lbltags.Text = "Tôi sẽ " + gig22.TenCongViec + " chỉ với 5 $";
            lblGioithieu.Text = gig22.TuGioiThieu;
            lblNgay.Text = gig22.SoNgayCanDeXong.ToString();
            lblTagss.Text = gig22.Tags;
            lblNguoidang.Text = gig22.VLGAccount.UsernameE;
            lblNguoiPostLQ.Text = gig22.VLGAccount.UsernameE;
            // Load Cờ
            quocgia_s.Src = "img/flags/" + gig22.VLGAccount.ipAddress + ".png";
            quocgia_s.Width = 20;
            quocgia_s.Border = 0;

            // load Like va DisLike
            lblSLNLike.Text = gig22.SoLuotThich.ToString();
            lblSLNDisL.Text = gig22.SoLuotKoThich.ToString();
            // Loadi Don hang
            var itemO = from or in _db4.Orders
                        where or.idGig == intG2
                        select or;
            lblDonHang.Text = itemO.Count().ToString();
            // Load Tittle
            Page.Title = "Vieclamgap.net | " + gig22.Category.TenCate.ToUpper() + " | Tôi sẽ " + gig22.TenCongViec + " chỉ với 5$";
            LiterTitle.Text = "\n<meta content=\"" + gig22.TenCongViec_KD + "," + gig22.Tags + "\" name=\"description\">" + "\n<meta content=\"" + gig22.TenCongViec_KD + "," + gig22.Tags + "\" name=\"content\">" + "\n<meta content=\"" + gig22.TenCongViec + "," + gig22.TenCongViec_KD + "," + gig22.Tags + "\" name=\"keywords\">";
            // Avatar nguoi dang
            if (!String.IsNullOrEmpty(gig22.VLGAccount.urlAvatar))
            {
                avar_s.Src = "avatar/" + gig22.VLGAccount.urlAvatar;
                avar_s.Width = 50;
                avar_s.Height = 45;

            }
            else
            {
                avar_s.Src = "avatar/noavatar.jpg";
                avar_s.Width = 50;
                avar_s.Height = 45;
            }
            var itemgig = from tc in _db4.Gigs
                          where tc.idGig == intG2 //&& tc.AnHien==true
                          select new { tc.idGig, tc.idAcc, tc.VLGAccount.UsernameE };
            Repeater1.DataSource = itemgig;
            Repeater1.DataBind();

            if (!String.IsNullOrEmpty(gig22.LinkVideo))
            {
                string[] tenfile = gig22.LinkVideo.Split('.');
                string tenfilekoduoi = tenfile[0];
                //antang.Src = "upload/" + gig22.UrlHinh1;
                // antang.Visible = false;
                //Literal1.Text = "<script type=\"text/javascript\">jwplayer(\"myplayer\").setup({skin:\"http://content.longtailvideo.com/skins/glow/glow.zip\",stretching:\"fill\",flashplayer: \"\",image: \"videos/hinh.jpg\",width: 270,height: 210,levels: [{ file: \"videos/" + gig22.LinkVideo + "\" },{ file: \"videos/" + tenfilekoduoi + ".webm\" }]});</script>";
                Literal1.Text = "<script type=\"text/javascript\">jwplayer(\"myplayer\").setup({skin:\"http://content.longtailvideo.com/skins/glow/glow.zip\",stretching:\"fill\",flashplayer: \"http://player.longtailvideo.com/player.swf\",image: \"/videos/hinh.jpg\",width: 270,height: 210,levels: [{ file: \"/videos/" + gig22.LinkVideo + "\" },{ file: \"/videos/" + tenfilekoduoi + ".webm\" }]});</script>";
            }
            else
            {
                tamhinh.InnerHtml = "<img src=\"/upload/" + gig22.UrlHinh1 + "\"  class=\"ava\" >";
                //antang.Src = "upload/" + gig22.UrlHinh1;
                // antang.Visible = false;
            }
            var itemCM = from cm in _db4.GigComments
                         where cm.AnHien == true && cm.idGig == intG2
                         select new { cm.idGig, cm.NoiDungBL, cm.NgayBL, cm.VLGAccount.UsernameE, cm.VLGAccount.idAcc, cm.Type };
            //select cm;

            SqlCommand cmd = (SqlCommand)_db4.GetCommand(itemCM);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            CollectionPager1.MaxPages = 1000;
            CollectionPager1.PageSize = 7;
            CollectionPager1.DataSource = dt.DefaultView;
            CollectionPager1.BindToControl = DataList1;
            DataList1.DataSource = CollectionPager1.DataSourcePaged;
            DataList1.DataBind();
            if (DataList1.Items.Count != 0)
            {
                bian.Attributes.CssStyle.Add("display", "none");
            }
            // bai viet lien quan
            var itemBVLQ = from bv in _db4.Gigs
                           where bv.idAcc == gig22.idAcc && bv.idGig != gig22.idGig && bv.TrangThai == (int)EnumGigStatus.GigHien && bv.Duyet == true
                           orderby bv.idAcc descending
                           select new { bv.idGig, bv.TenCongViec, bv.Category.idCategory,bv.Category.TenCate_E,bv.TenCongViec_KD };
            rptLienQuan.DataSource = itemBVLQ;
            rptLienQuan.DataBind();

            if (rptLienQuan.Items.Count != 0)
            {
                bian2.Attributes.CssStyle.Add("display", "none");
            }
        }
        catch
        {
            Response.Redirect(ResolveClientUrl("accessDenied.aspx"));
        }
        finally
        {

        }
    }

    void loadFacebook()
    {
        string str = ("http://" + Request.ServerVariables["SERVER_NAME"] + Request.RawUrl.ToString());
        string html = "";
        html += "<iframe src=\"//www.facebook.com/plugins/like.php?href=" + str + "&amp;send=false&amp;layout=button_count&amp;width=290&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=80\"";
        html += "scrolling=\"no\" frameborder=\"0\" style=\"border: none; overflow: hidden; width: 80px;height: 19px;\" allowtransparency=\"true\"></iframe>";
        //face_.InnerHtml = html;
    }
    //protected void btnBL_Click1(object sender, EventArgs e)
    //{
    //    if (CaptchaCtrl1.IsValid == true)
    //    {
    //        string idtaikhoan = cm66.Customer.idAcc.ToString();
    //        string idG2 = Request.QueryString["g"];
    //        int intIdG2 = int.Parse(idG2);

    //        try
    //        {
    //            GigComment g2c = new GigComment()
    //            {
    //                TieuDe = txtTieuDeCM.Text.Trim(),
    //                NgayBL = DateTime.Now,
    //                idAcc = int.Parse(idtaikhoan),
    //                HoTen = txtHoTenCM.Text.Trim(),
    //                CmEmail = txtEmailCMa.Text.Trim(),
    //                idGig = intIdG2,
    //                NoiDungBL = txtNDCM.Text.Trim(),
    //                AnHien = true
    //            };
    //            _db4.GigComments.InsertOnSubmit(g2c);
    //            _db4.SubmitChanges();

    //            // tao session thử
    //            //  Session["idGig"] = "true";

    //            Response.Redirect(Request.Url.AbsoluteUri);
    //            txtTieuDeCM.Text = "";
    //            txtNDCM.Text = "";
    //            txtHoTenCM.Text = "";
    //            txtEmailCMa.Text = "";

    //            upPanel.Update();
    //            //liComment.Text = "<script type=\"text/javascript\">alert('Bình luận thành công ! \n. Bình luận của bạn đang được duyệt ... !')</script>";
    //        }
    //        catch
    //        {
    //            liComment.Text = "<script type=\"text/javascript\">alert('Không thể bình luận vào lúc này')</script>";
    //            upPanel.Update();
    //        }
    //    }
    //    else
    //    {
    //        // Response.Redirect(Request.Url.AbsoluteUri);
    //        return;
    //    }
    //}
    public string UsNa
    {
        get
        {
            try
            {
                return cm66.Customer.UsernameE;
            }
            catch
            {
                return "";
            }
        }
    }
}
